import request from '@/utils/request'

export let mockInventoryList = [
  { id: 1, name: '商品A', code: 'A001', category: '食品', stock: 100, stockStatus: 'normal' },
  { id: 2, name: '商品B', code: 'B002', category: '饮料', stock: 10, stockStatus: 'low' }
];
let mockInventoryInList = [
  { id: 1, productId: 1, productName: '商品A', productCode: 'A001', quantity: 20, operator: '管理员', createTime: '2024-06-01 10:00:00', remark: '补货' }
];
let mockInventoryOutList = [
  { id: 1, productId: 2, productName: '商品B', productCode: 'B002', quantity: 5, operator: '管理员', createTime: '2024-06-02 11:00:00', remark: '销售出库' }
];

export function getInventoryList() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: { total: mockInventoryList.length, items: mockInventoryList }
      });
    }, 300);
  });
}

export function getInventoryDetail(id) {
  return request({
    url: `/inventory/${id}`,
    method: 'get'
  })
}

export function addInventory(data) {
  return new Promise(resolve => {
    setTimeout(() => {
      const newItem = { ...data, id: Date.now() };
      mockInventoryList.unshift(newItem);
      resolve({ code: 20000, data: newItem });
    }, 300);
  });
}

export function updateInventory(id, data) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockInventoryList = mockInventoryList.map(item => item.id === id ? { ...item, ...data } : item);
      resolve({ code: 20000, data: { ...data, id } });
    }, 300);
  });
}

export function deleteInventory(id) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockInventoryList = mockInventoryList.filter(item => item.id !== id);
      resolve({ code: 20000 });
    }, 300);
  });
}

export function getInventoryInList() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: { total: mockInventoryInList.length, items: mockInventoryInList }
      });
    }, 300);
  });
}

export function addInventoryIn(data) {
  return new Promise(resolve => {
    setTimeout(() => {
      const now = new Date();
      const year = now.getFullYear();
      const month = ('0' + (now.getMonth() + 1)).slice(-2);
      const day = ('0' + now.getDate()).slice(-2);
      const hours = ('0' + now.getHours()).slice(-2);
      const minutes = ('0' + now.getMinutes()).slice(-2);
      const seconds = ('0' + now.getSeconds()).slice(-2);
      const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

      const newItem = { ...data, id: data.id, createTime: formattedTime, operator: '管理员' };
      mockInventoryInList.unshift(newItem);
      resolve({ code: 20000, data: newItem });
    }, 300);
  });
}

export function deleteInventoryIn(id) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockInventoryInList = mockInventoryInList.filter(item => item.id !== id);
      resolve({ code: 20000 });
    }, 300);
  });
}

export function getInventoryOutList() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: { total: mockInventoryOutList.length, items: mockInventoryOutList }
      });
    }, 300);
  });
}

export function addInventoryOut(data) {
  return new Promise(resolve => {
    setTimeout(() => {
      const now = new Date();
      const year = now.getFullYear();
      const month = ('0' + (now.getMonth() + 1)).slice(-2);
      const day = ('0' + now.getDate()).slice(-2);
      const hours = ('0' + now.getHours()).slice(-2);
      const minutes = ('0' + now.getMinutes()).slice(-2);
      const seconds = ('0' + now.getSeconds()).slice(-2);
      const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

      const newItem = { ...data, id: Date.now(), createTime: formattedTime, operator: '管理员' };
      mockInventoryOutList.unshift(newItem);
      resolve({ code: 20000, data: newItem });
    }, 300);
  });
}

export function deleteInventoryOut(id) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockInventoryOutList = mockInventoryOutList.filter(item => item.id !== id);
      resolve({ code: 20000 });
    }, 300);
  });
} 